package com.ruibang.glass.quality.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruibang.glass.common.constants.RedisKey;
import com.ruibang.glass.common.domain.file.FileBusiness;
import com.ruibang.glass.quality.domain.req.IntervalPaperReq;
import com.ruibang.glass.quality.domain.req.QualityStripeReq;
import com.ruibang.glass.quality.entity.FinishIntervalPaper;
import com.ruibang.glass.quality.entity.IntervalPaper;
import com.ruibang.glass.quality.entity.QualityStripe;
import com.ruibang.glass.quality.mapper.IntervalPaperMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruibang.glass.quality.service.IntervalPaperService;
import com.teaming.cloud.framework2.autoconfigure.redis.RedisSingleService;
import com.teaming.cloud.framework2.autoconfigure.redis.RedisSingleService;
import com.teaming.cloud.framework2.common.constant.CommonConstant;
import com.teaming.cloud.framework2.common.model.ResultBody;
import com.teaming.cloud.framework2.common.pojo.PageRequest;
import com.teaming.cloud.framework2.common.pojo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 间隔纸受入信息 服务实现类
 * </p>
 *
 * @author wujie
 * @since 2024-03-18
 */
@Service
public class IntervalPaperServiceImpl extends ServiceImpl<IntervalPaperMapper, IntervalPaper> implements IntervalPaperService {

//    @Autowired
//    private RedisSingleService redisService;

    @Autowired
    private RedisSingleService redisService;

    @Override
    public PageResult<IntervalPaper> queryPage(PageRequest<IntervalPaperReq> pageRequest) {
        final IntervalPaperReq param = pageRequest.getParam();
        final LambdaQueryWrapper<IntervalPaper> lambda = new QueryWrapper<IntervalPaper>().lambda();
        this.buildCondition(lambda,param);
        final IPage<IntervalPaper> page = this.page(new Page<>(pageRequest.getPageNo(), pageRequest.getPageSize()), lambda);
        if(page!=null&& CollectionUtils.isNotEmpty(page.getRecords())){
            Map<Object, Object> userMap = redisService.hmget(RedisKey.USER_GROUP_TYPE);
            for (IntervalPaper record : page.getRecords()) {
                if(userMap!=null){
                    record.setCheckUserName(userMap.get(record.getCheckUser())==null?"":userMap.get(record.getCheckUser()).toString());
                    record.setMeasureUserName(userMap.get(record.getMeasureUser())==null?"":userMap.get(record.getMeasureUser()).toString());
                    record.setSurplusUserName(userMap.get(record.getSurplusUser())==null?"":userMap.get(record.getSurplusUser()).toString());
                }
            }
        }

        PageResult<IntervalPaper> pr = new PageResult(page);
        return pr;
    }

    @Override
    public void delete(String id) {
        lambdaUpdate().eq(IntervalPaper::getPaperId, id).set(IntervalPaper::getIsDelete, CommonConstant.ONE_STR).update();
    }


    @Override
    public List<IntervalPaper> queryIntervalPaper(IntervalPaperReq stripeReq) {
        final LambdaQueryWrapper<IntervalPaper> lambda = new QueryWrapper<IntervalPaper>().lambda();
        this.buildCondition(lambda,stripeReq);
        return this.list(lambda);
    }

    /**
     * 构造查询条件
     * @param lambda
     * @param param
     */
    private void buildCondition(LambdaQueryWrapper<IntervalPaper> lambda, IntervalPaperReq param){
        lambda.eq(IntervalPaper::getIsDelete, CommonConstant.ZERO_STR);
        if(null == param) return;
        if(param.getCheckStartTime()!=null&&param.getCheckEndTime()!=null){
            lambda.ge(IntervalPaper::getCheckTime, param.getCheckStartTime());
            lambda.le(IntervalPaper::getCheckTime, param.getCheckEndTime());
        }
        lambda.orderBy(true,false, IntervalPaper::getCheckTime);
    }
}
